package com.wht.controller.admin;

import com.wht.domain.Admin;
import com.wht.service.admin.AdminLoginService;
import com.wht.service.admin.impl.AdminLoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wanghaotian
 * time:2022/11/21,0021 19:34
 * description:
 */

@WebServlet("/AdminLoginController")
public class AdminLoginController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        AdminLoginService adminLoginService = new AdminLoginServiceImpl();
        Admin admin = adminLoginService.adminLogin(username,password);
        if(admin != null){
            Cookie cookie = new Cookie("admin", admin.getUsername());
            cookie.setMaxAge(3600*24*7);
            resp.addCookie(cookie);
            req.getSession().setAttribute("admin", admin);
            resp.sendRedirect("/FindAllMusicController");
        }else {
            req.setAttribute("msg", "用户名或密码错误");
            req.getRequestDispatcher("/msg.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}